<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>查询地理形状 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/querying-geo-shapes.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/querying-geo-shapes.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/querying-geo-shapes.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/querying-geo-shapes.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="geoloc.html">地理位置</a></span>
»
<span class="breadcrumb-link"><a href="geo-shapes.html">地理形状</a></span>
»
<span class="breadcrumb-node">查询地理形状</span>
</div>
<div class="navheader">
<span class="prev">
<a href="indexing-geo-shapes.html">« 索引地理形状</a>
</span>
<span class="next">
<a href="indexed-geo-shapes.html">在查询中使用已索引的形状 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="querying-geo-shapes"></a>查询地理形状<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/340_Geoshapes/76_Querying_geo_shapes.asciidoc">edit</a>
</h2>
</div></div></div>
<p> <a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-geo-shape-query.html" class="ulink" target="_top"><code class="literal">geo_shape</code> 查询</a>不寻常的地方在于，它允许我们使用形状来做查询，而不仅仅是坐标点。</p>
<p>举个例子，当我们的用户刚刚迈出阿姆斯特丹中央火车站时，我们可以用如下方式，查询出方圆 1km 内的所有地标：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /attractions/landmark/_search
{
  "query": {
    "geo_shape": {
      "location": { <a id="CO245-1"></a><i class="conum" data-value="1"></i>
        "shape": { <a id="CO245-2"></a><i class="conum" data-value="2"></i>
          "type":   "circle", <a id="CO245-3"></a><i class="conum" data-value="3"></i>
          "radius": "1km",
          "coordinates": [ <a id="CO245-4"></a><i class="conum" data-value="4"></i>
            4.89994,
            52.37815
          ]
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO245-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>查询使用 <code class="literal">location</code> 字段中的地理形状。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO245-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>查询中的形状是由 <code class="literal">shape</code> 键对应的内容表示的。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO245-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>形状是一个半径为 1km 的圆形。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO245-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>安姆斯特丹中央火车站入口的坐标点。</p>
</td>
</tr>
</table>
</div>
<p>默认情况下，查询（或者过滤器 —— 做同样的工作）会从已索引的形状中寻找与指定形状有交集的部分。此外，可以把 <code class="literal">relation</code> 字段设置为 <code class="literal">disjoint</code> 来查找与指定形状不相交的部分，或者设置为 <code class="literal">within</code> 来查找完全落在查询形状中的。</p>
<p>举个例子，我们可以查找阿姆斯特丹中心区域所有的地标：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /attractions/landmark/_search
{
  "query": {
    "geo_shape": {
      "location": {
        "relation": "within", <a id="CO246-1"></a><i class="conum" data-value="1"></i>
        "shape": {
          "type": "polygon",
          "coordinates": [[ <a id="CO246-2"></a><i class="conum" data-value="2"></i>
              [4.88330,52.38617],
              [4.87463,52.37254],
              [4.87875,52.36369],
              [4.88939,52.35850],
              [4.89840,52.35755],
              [4.91909,52.36217],
              [4.92656,52.36594],
              [4.93368,52.36615],
              [4.93342,52.37275],
              [4.92690,52.37632],
              [4.88330,52.38617]
            ]]
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO246-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>只匹配完全落在查询形状中的已索引的形状。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO246-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>这个多边形表示安姆斯特丹中心区域。</p>
</td>
</tr>
</table>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="indexing-geo-shapes.html">« 索引地理形状</a>
</span>
<span class="next">
<a href="indexed-geo-shapes.html">在查询中使用已索引的形状 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>